<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      ul {
        list-style: none;
        padding: 0;
      }
      li {
        background-color: skyblue;
        margin-top: 10px;
      }
    </style>
  </head>
  <body>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
    </ul>
    <script>
      var ul = document.querySelector('ul')
      ul.addEventListener('click', function (event) {
        //判定当前事件源是否是ul本身
        if (event.target !== this) {
          var lis = ul.getElementsByTagName('li')
          //将其他同类背景色全部重置
          for (var i = 0; i < lis.length; i++) {
            lis[i].style.backgroundColor = 'skyblue'
          }
          //改变事件源背景色
          event.target.style.backgroundColor = 'red'
        }
      })
    </script>
  </body>
</html>
